//
// File: hist.cpp
//
// MATLAB Coder version            : 5.4
// C/C++ source code generated on  : 23-Apr-2025 16:26:18
//

// Include Files
#include "hist.h"
#include "rt_nonfinite.h"
#include "coder_array.h"
#include <algorithm>
#include <cmath>
#include <cstring>
#include <math.h>
#include <string.h>

// Function Definitions
//
// Arguments    : const ::coder::array<double, 1U> &Y
//                double no[32]
//                double xo[32]
// Return Type  : void
//
namespace coder {
void hist(const ::coder::array<double, 1U> &Y, double no[32], double xo[32])
{
  double nn[33];
  int low_ip1;
  if (Y.size(0) == 0) {
    for (int i{0}; i < 32; i++) {
      xo[i] = static_cast<double>(i) + 1.0;
      no[i] = 0.0;
    }
  } else {
    double edges[33];
    double delta2;
    double maxy;
    double miny;
    int i;
    unsigned int k;
    int ny;
    boolean_T exitg1;
    ny = Y.size(0);
    k = 1U;
    exitg1 = false;
    while ((!exitg1) && (k <= static_cast<unsigned int>(ny))) {
      delta2 = Y[static_cast<int>(k) - 1];
      if ((!std::isinf(delta2)) && (!std::isnan(delta2))) {
        exitg1 = true;
      } else {
        k++;
      }
    }
    if (k > static_cast<unsigned int>(Y.size(0))) {
      miny = 0.0;
      maxy = 0.0;
    } else {
      miny = Y[static_cast<int>(k) - 1];
      maxy = Y[static_cast<int>(k) - 1];
      while (k <= static_cast<unsigned int>(ny)) {
        delta2 = Y[static_cast<int>(k) - 1];
        if ((!std::isinf(delta2)) && (!std::isnan(delta2))) {
          if (delta2 < miny) {
            miny = Y[static_cast<int>(k) - 1];
          }
          if (delta2 > maxy) {
            maxy = delta2;
          }
        }
        k++;
      }
    }
    if (miny == maxy) {
      miny = (miny - 16.0) - 0.5;
      maxy = (maxy + 16.0) - 0.5;
    }
    edges[32] = maxy;
    edges[0] = miny;
    if (miny == -maxy) {
      delta2 = maxy / 32.0;
      for (int b_k{0}; b_k < 31; b_k++) {
        edges[b_k + 1] =
            (2.0 * (static_cast<double>(b_k) + 2.0) - 34.0) * delta2;
      }
      edges[16] = 0.0;
    } else if (((miny < 0.0) != (maxy < 0.0)) &&
               ((std::abs(miny) > 8.9884656743115785E+307) ||
                (std::abs(maxy) > 8.9884656743115785E+307))) {
      double delta1;
      delta1 = miny / 32.0;
      delta2 = maxy / 32.0;
      for (int b_k{0}; b_k < 31; b_k++) {
        edges[b_k + 1] = (miny + delta2 * (static_cast<double>(b_k) + 1.0)) -
                         delta1 * (static_cast<double>(b_k) + 1.0);
      }
    } else {
      double delta1;
      delta1 = (maxy - miny) / 32.0;
      for (int b_k{0}; b_k < 31; b_k++) {
        edges[b_k + 1] = miny + (static_cast<double>(b_k) + 1.0) * delta1;
      }
    }
    delta2 = (edges[1] - edges[0]) / 2.0;
    for (i = 0; i < 32; i++) {
      xo[i] = edges[i] + delta2;
    }
    edges[0] = rtMinusInf;
    edges[32] = rtInf;
    for (int b_k{0}; b_k < 31; b_k++) {
      maxy = edges[b_k + 1];
      delta2 = std::abs(maxy);
      if ((!std::isinf(delta2)) && (!std::isnan(delta2))) {
        if (delta2 <= 2.2250738585072014E-308) {
          delta2 = 4.94065645841247E-324;
        } else {
          frexp(delta2, &low_ip1);
          delta2 = std::ldexp(1.0, low_ip1 - 53);
        }
      } else {
        delta2 = rtNaN;
      }
      edges[b_k + 1] = maxy + delta2;
    }
    std::memset(&nn[0], 0, 33U * sizeof(double));
    i = Y.size(0);
    for (int b_k{0}; b_k < i; b_k++) {
      ny = 0;
      if (!std::isnan(Y[b_k])) {
        if ((Y[b_k] >= edges[0]) && (Y[b_k] < edges[32])) {
          int high_i;
          ny = 1;
          low_ip1 = 2;
          high_i = 33;
          while (high_i > low_ip1) {
            int mid_i;
            mid_i = (ny + high_i) >> 1;
            if (Y[b_k] >= edges[mid_i - 1]) {
              ny = mid_i;
              low_ip1 = mid_i + 1;
            } else {
              high_i = mid_i;
            }
          }
        }
        if (Y[b_k] == edges[32]) {
          ny = 33;
        }
      }
      if (ny > 0) {
        nn[ny - 1]++;
      }
    }
    std::copy(&nn[0], &nn[32], &no[0]);
    no[31] += nn[32];
  }
}

} // namespace coder

//
// File trailer for hist.cpp
//
// [EOF]
//
